package com.crm.service.impl.returnMoney;

import com.crm.dao.mapper.customMapper.returnMoneyManagerCustomMapper.BankFlowCustomMapper;
import com.crm.dao.mapper.orderManager.CustomerProductOrderMapper;
import com.crm.dao.mapper.returnMoney.BankFlowMapper;
import com.crm.dao.mapper.returnMoney.BankFlowRecordMapper;
import com.crm.model.entity.orderManager.CustomerProductOrder;
import com.crm.model.entity.orderManager.CustomerProductOrderExample;
import com.crm.model.entity.returnMoney.*;
import com.crm.model.qo.returnMoney.BankFlowQO;
import com.crm.model.vo.returnMoney.*;
import com.crm.service.returnMoney.BankFlowDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author yuanyunfeng
 * @date 2021/7/26 11:05
 */
@Service
public class BankFlowDbServiceImpl implements BankFlowDbService {

    @Autowired
    private BankFlowCustomMapper bankFlowCustomMapper;
    @Autowired
    private BankFlowMapper bankFlowMapper;
    @Autowired
    private CustomerProductOrderMapper orderMapper;
    @Autowired
    private BankFlowRecordMapper recordMapper;


    @Override
    public boolean existTransactioNo(String transactionNo, String transactionDate, String creditMoney) {
        BankFlowExample bankEx = new BankFlowExample();
        bankEx.createCriteria().andTransactionNoEqualTo(transactionNo).andTransactionDateEqualTo(transactionDate).andCreditMoneyEqualTo(new BigDecimal(creditMoney)).andOperateFlagEqualTo("ok");
        List<BankFlow> transList = bankFlowMapper.selectByExample(bankEx);
        if (transList != null && transList.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<ProductOrderInfoVO> selectProductOrderId(String companyId, String customerName) {
        return bankFlowCustomMapper.selectProductOrderId(companyId, customerName);
    }

    @Override
    public int insertBankFlow(BankFlow bankFlow) {
        return bankFlowMapper.insertSelective(bankFlow);
    }


    @Override
    public String queryOrderId(String orderNumber) {
        CustomerProductOrderExample orderExample = new CustomerProductOrderExample();
        orderExample.createCriteria().andOrderNumberEqualTo(orderNumber);
        List<CustomerProductOrder> customerProductOrders = orderMapper.selectByExample(orderExample);
        if (customerProductOrders != null && customerProductOrders.size() > 0) {
            return customerProductOrders.get(0).getProductOrderId();
        }
        return null;
    }

    @Override
    public boolean deleteByFlowId(String flowId) {
        return bankFlowMapper.deleteByPrimaryKey(flowId) > 0;
    }


    /**
     * 根据流水号、公司查询流水信息
     * @param serial
     * @param companyId
     * @return
     */
    @Override
    public List<BankFlow> selectByTransactionNo(String serial, String companyId) {
        BankFlowExample bfExample = new BankFlowExample();
        bfExample.createCriteria().andTransactionNoEqualTo(serial).andCompanyIdEqualTo(companyId).andOperateFlagEqualTo("ok");
        return bankFlowMapper.selectByExample(bfExample);
    }

    /**
     * 通过订单编号的公司，流水号查询流水可用余额
     * @param serial
     * @param companyId
     * @return
     */
    @Override
    public BigDecimal selectCountAvailableAmount(String serial, String companyId) {
        return bankFlowCustomMapper.selectCountAvailableAmount(serial,companyId);
    }

    @Override
    public List<BankFlowReturnLogVO> querySubAvailableAmount(String flowId) {
        return bankFlowCustomMapper.querySubAvailableAmount(flowId);
    }

    @Override
    public BankFlow queryByTransactionNo(String transactionNo, String transactionDate, BigDecimal creditMoney) {
        BankFlowExample bfExample = new BankFlowExample();
        bfExample.createCriteria().andTransactionNoEqualTo(transactionNo).andTransactionDateEqualTo(transactionDate).andCreditMoneyEqualTo(creditMoney);
        return bankFlowMapper.selectByExample(bfExample).get(0);
    }

    @Override
    public void insertBankFlowRecord(BankFlowRecord record) {
        recordMapper.insertSelective(record);
    }

    @Override
    public void updateRelationState(BankFlow bankFlow) {
        bankFlowMapper.updateByPrimaryKeySelective(bankFlow);
    }

    @Override
    public int cancelUpload(String accountId, Integer batchNo, Integer readRow) {
        BankFlowExample bankEx = new BankFlowExample();
        bankEx.createCriteria().andCreatePeopleIdEqualTo(accountId).andBatchNoEqualTo(batchNo).andRowNumberGreaterThan(readRow);
        return bankFlowMapper.deleteByExample(bankEx);
    }

    @Override
    public int getReturnMoneyDayNum() {
        return bankFlowCustomMapper.getReturnMoneyDayNum();
    }

    @Override
    public List<BankFlowQueryVO> queryBankList(String currentCompanyId, BankFlowQO bankFlowQO) {
        return bankFlowCustomMapper.queryBankList(currentCompanyId, bankFlowQO);
    }

    @Override
    public BankFlowDataVO queryMonthMoney(String createPeopleId, String queryYear, String queryMonth) {
        return bankFlowCustomMapper.queryMonthMoney(createPeopleId, queryYear, queryMonth);
    }

    @Override
    public List<BankFlowRelateVO> queryBankRelatedByFlowId(String flowId) {
        return bankFlowCustomMapper.queryBankRelatedByFlowId(flowId);
    }

    @Override
    public List<BankFlowReturnLogVO> queryOperatorLog(String flowId, String orderNumber) {
        return bankFlowCustomMapper.queryOperatorLog(flowId, orderNumber);
    }

    @Override
    public List<BankFlowReturnLogVO> queryCustomerPayLog(String flowId, String payNumber) {
        return bankFlowCustomMapper.queryCustomerPayLog(flowId, payNumber);
    }

    @Override
    public int updateByPrimaryKeySelective(BankFlow bankFlow) {
        return bankFlowMapper.updateByPrimaryKeySelective(bankFlow);
    }

    @Override
    public List<BankOrderReturn> queryReturnMoney(String flowId, String orderNumber) {
        return bankFlowCustomMapper.queryReturnMoney(flowId, orderNumber);
    }

    @Override
    public List<BankOrderReturn> queryPayMoney(String flowId, String payNumber) {
        return bankFlowCustomMapper.queryPayMoney(flowId, payNumber);
    }

    @Override
    public BankFlow selectByFlowId(String flowId) {
        return bankFlowMapper.selectByPrimaryKey(flowId);
    }


}
